package io.renren.datasources;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;

/**多数据源事务配置 暂时屏蔽bug
 * @author zh
 * @date 2018/8/2110:29
 */
//@Configuration
public class TransactionManager implements TransactionManagementConfigurer {

    @Autowired
    @Qualifier("firstDataSource")
    private DataSource firstDataSource;

    /******配置事务管理********/

    @Bean(name="firstTransactionManager")
    public PlatformTransactionManager firstTransactionManager(@Qualifier("firstDataSource")DataSource firstDataSource) {
        return new DataSourceTransactionManager(firstDataSource);
    }

    @Bean(name="secondTransactionManager")
    public PlatformTransactionManager secondTransactionManager(@Qualifier("secondDataSource")DataSource secondDataSource) {
        return new DataSourceTransactionManager(secondDataSource);
    }

    // 实现接口 TransactionManagementConfigurer 方法，其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(firstDataSource);
    }
}
